<!--
* description: 邀约登记
* author:wangjuan
* createdDate:2020-3-5
-->
<template>
  <section class="editSetInEdit">
    <el-dialog
      v-dialogDrag
      :title="titleName"
      :visible="popupsVisible"
      :append-to-body="true"
      width="40%"
      :close-on-click-modal="false"
      @close="closeEdit"
    >
      <dialogHeader
        slot="title"
        :title="titleName"
        :style-type="$store.state.app.dialogType"
      />
      <div class="app-container app-container-table">
        <one-table-template
          ref="multipleTable"
          :dynamic-components="tableComponents"
          :dynamic-form-fields="formField"
        />
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          :icon="comp.icon"
          :fixed="true"
          @click="comp.clickEvent"
        >{{ comp.text }}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>

import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import { formMixins } from '@/components/mixins/formMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import api from '@/api/se/serviceActivities'

export default {
  components: {
    OneTableTemplate,
    dialogHeader: () => import('@/components/dialogHeader')
  },

  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins, formMixins],

  props: {
    // 弹出是否显示
    popupsVisible: { type: Boolean, default: false },
    dynamicEditRowData: { type: Object, default: function() { return {} } }
  },

  data() {
    const viewPageCode = 'save-edit'
    return {
      titleName: '客户邀约',

      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          icon: 'iconfont  icon-confirm',
          clickEvent: () => this.invite('success'),
          text: '邀约成功',
          position: 'right'
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          icon: 'iconfont  icon-cancel',
          clickEvent: () => this.invite('failed'),
          text: '邀约失败'
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          : [
            {
              prop: 'releaseId',
              label: '主键ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              compKey: 'compKey1',
              labelName: '客户编号：',
              codeField: 'dlrCustNo',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              disabled: true,
              isMust: true,
              span: 24
            },
            {
              compKey: 'compKey2',
              labelName: '客户名称：',
              codeField: 'custName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              disabled: true,
              isMust: true,
              span: 24
            },
            {
              compKey: 'compKey3',
              labelName: 'VIN码：',
              codeField: 'vin',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              disabled: true,
              isMust: true,
              span: 24
            },
            {
              compKey: 'compKey4',
              labelName: '邀约状态：',
              codeField: 'inviteStateCn',
              component: () => import('@/components/org/isEnable/isEnableOne'),
              type: 'dropdownList',
              isMust: true,
              disabled: true,
              span: 24
            },
            {
              compKey: 'compKey5',
              labelName: '邀约记录：',
              codeField: 'inviteResult',
              component: () => import('@/components/org/commonInput/textareaInput'),
              type: 'inputText',
              isMust: true,
              span: 24
            }
          ],
      // 表单数据（无需赋值，由混入对象赋值）
      formField: this.$utils.getFormField(this, {
        vin: '',
        custName: '',
        inviteResult: '',
        inviteStateCn: '',
        dlrCustNo: '',
        updateControlId: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
    }
  },
  watch: {
    popupsVisible(val) {
      // debugger
      if (val) {
        this.formField = this.dynamicEditRowData
        console.log(this.formField)
      }
    }
  },

  methods: {
    // 关闭弹窗
    closeEdit(type) {
      this.$emit('closeInEdit', type)
    },

    invite(type) {
      this.$refs.multipleTable.listLoading = true
      let apiConfigs
      if (type === 'success') {
        apiConfigs = api.eduitActionStatePass
      } else if (type === 'failed') {
        apiConfigs = api.eduitActionStateUnPass
      }
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: apiConfigs,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: this.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          this.$message({
            message: response.msg,
            type: 'success',
            duration: 1000
          })
          this.closeEdit()
          this.$parent.queryTable(1)
        } else {
          // 失败
          this.$message({
            message: response.msg,
            type: 'warn',
            duration: 1000
          })
        }
        this.closeEdit()
      })
    }

  }
}
</script>
